package com.young.operation.components;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import java.sql.SQLException;

public class DruidDataSourceCreator {

    public DruidDataSourceCreator() {
        
    }

    public static DruidDataSource create(DataSourceProperties dataSourceProperties, HighAvailableDruidConfig druidConfig) throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setName(dataSourceProperties.getName() + "-" + System.identityHashCode(dataSource));
        dataSource.setUrl(dataSourceProperties.getUrl());
        dataSource.setUsername(dataSourceProperties.getUsername());
        dataSource.setPassword(dataSourceProperties.getPassword());
        commonAvailableConfig(dataSource, druidConfig);
        dataSource.init();
        return dataSource;
    }

    public static DruidDataSource create(String name, String url, String username, String password, HighAvailableDruidConfig druidConfig) throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setName(name + "-" + System.identityHashCode(dataSource));
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        commonAvailableConfig(dataSource, druidConfig);
        dataSource.init();
        return dataSource;
    }

    private static void commonAvailableConfig(DruidDataSource dataSource, HighAvailableDruidConfig druidConfig) throws SQLException {
        dataSource.setDriverClassName(druidConfig.getDriverClassName());
        dataSource.setConnectProperties(druidConfig.getConnectProperties());
        dataSource.setConnectionProperties(druidConfig.getConnectionProperties());
        dataSource.setInitialSize(druidConfig.getInitialSize());
        dataSource.setMaxActive(druidConfig.getMaxActive());
        dataSource.setMinIdle(druidConfig.getMinIdle());
        dataSource.setMaxWait(druidConfig.getMaxWait());
        dataSource.setValidationQuery(druidConfig.getValidationQuery());
        dataSource.setValidationQueryTimeout(druidConfig.getValidationQueryTimeout());
        dataSource.setTestOnBorrow(druidConfig.isTestOnBorrow());
        dataSource.setTestOnReturn(druidConfig.isTestOnReturn());
        dataSource.setTestWhileIdle(druidConfig.isTestWhileIdle());
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(druidConfig.getMaxPoolPreparedStatementPerConnectionSize());
        dataSource.setQueryTimeout(druidConfig.getQueryTimeout());
        dataSource.setTransactionQueryTimeout(druidConfig.getTransactionQueryTimeout());
        dataSource.setTimeBetweenEvictionRunsMillis(druidConfig.getTimeBetweenEvictionRunsMillis());
        dataSource.setMinEvictableIdleTimeMillis(druidConfig.getMinEvictableIdleTimeMillis());
        dataSource.setMaxEvictableIdleTimeMillis(druidConfig.getMaxEvictableIdleTimeMillis());
        dataSource.setTimeBetweenConnectErrorMillis(druidConfig.getTimeBetweenConnectErrorMillis());
        dataSource.setFilters(druidConfig.getFilters());
        dataSource.setUseGlobalDataSourceStat(druidConfig.getUseGlobalDataSourceStat());
    }

}
